package com.jxgk.controller;


import com.jxgk.pojo.Dept;
import com.jxgk.pojo.Dto;
import com.jxgk.pojo.User;
import com.jxgk.pojo.Zw;
import com.jxgk.service.UserService;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;

    @RequestMapping("{page}") //跳转页面
    public String showPage(@PathVariable String page){
        return page;
    }

    @RequestMapping("/findAll")
    public String findAll(Model model){
      List<Dto> user = userService.findAll();
      model.addAttribute("list",user);
      return "user";
    }


    @RequestMapping("del")
    public String del(Integer id){
        userService.del(id);
        return "redirect:/user/findAll";
    }


    @RequestMapping("fid")
    public String fid(Integer uid,Model model){
       User user= userService.fid(uid);
        model.addAttribute("user",user);
        List<Dept> dept =userService.findDept();
        model.addAttribute("dept",dept);
        List<Zw> zw =userService.findZw();
        model.addAttribute("zw",zw);
        return "update";
    }




    @RequestMapping("add")
    public String add(User user){
     if (user!=null &&user.getUid()!=null){
         userService.update(user);
         return "redirect:/user/findAll";
     }else {
         userService.add(user);
         return "redirect:/user/findAll";
     }
    }



    @RequestMapping("addf")
    public String addf(Model model){
        List<Dept> dept =userService.findDept();
        model.addAttribute("dept",dept);
        List<Zw> zw =userService.findZw();
        model.addAttribute("zw",zw);
        return "add";
    }

}
